﻿var table;
$(document).ready(function () {
    //  $("#rightResources").addClass("active");
    table = $('#LibraryTable').dataTable({
        "bServerSide": true, //这个用来指明是通过服务端来取数据
        "sAjaxSource": "/Library/GetAllInfos", //这个是请求的地址
      //   "bProcessing": true, // 是否显示取数据时的那个等待提示
        "iDisplayLength": 15,
        "searching": false,
        "aoColumns": [
            { "mDataProp": "LibraryID", "sTitle": "LibraryID", "sDefaultContent": "" },
            { "mDataProp": "LibraryName", "sTitle": "题库名称", "sDefaultContent": "" },
            { "mDataProp": "CreateTime", "sTitle": "最后修改时间", "sDefaultContent": "" },
            { "mDataProp": "CreateUserID", "sTitle": "最后修改人", "sDefaultContent": "" },
            { "mDataProp": "Remarks", "sTitle": "备注", "sDefaultContent": "" },
             { "mDataProp": "Remarks", "sTitle": "操作", "sDefaultContent": "" }
        ],
        "aoColumnDefs": [
             {
                 "targets": [5], // 目标列位置，下标从0开始
                 "orderable": false,
                 //  "visible": $("#hidOneActionPageIsEdit").val() == "1" ? true : false,
                 "render": function (data, type, full) { // 返回自定义内容
                     return "<a href='javascript:void(0)' class=\"btn btn-primary\" style=\"text-decoration:none;outline:none\" onclick=\"javascript:Deleteinfo('" + full.LibraryID + "')\" class='Link'>编辑</a><a href='javascript:void(0)' class=\"btn btn-primary\" style=\"text-decoration:none;outline:none\" onclick=\"javascript:Deleteinfo('" + full.LibraryID + "')\" class='Link'>Delete</a>";
                 }
             },
               {
                   "targets": [2], // 目标列位置，下标从0开始
                   "data": "CreateTime", // 数据列名
                   "render": function (data, type, full) { // 返回自定义内容
                       var date = new Date(parseInt(data.replace("/Date(", "").replace(")/", ""), 10));
                       var year = date.getFullYear();
                       var month = (date.getMonth() + 1).toString();
                       var day = (date.getDate()).toString();
                       if (month.length == 1) {
                           month = "0" + month;
                       }
                       if (day.length == 1) {
                           day = "0" + day;
                       }
                       return year + '-' + month + '-' + day;
                   }
               },
            {
                "targets": 0, //第一列隐藏
                "visible": false,
                "searchable": false
            }
        ],
        "fnServerParams": function (aoData) { //查询条件
            aoData.push(
                { "name": "libraryname", "value": $("#libraryname").val() }

            );
        },
        "bLengthChange": false,
        "sPaginationType": "full_numbers"
    });

    /* Add a click handler to the rows - this could be used as a callback */
    $('#LibraryTable tbody').on('click', 'tr', function () {
        $(this).toggleClass('selected');
    });

    $('#delBut').click(function () {
        var selectdata = $("#LibraryTable").DataTable().rows('.selected').data();
        var selectleng = selectdata.length;
        var idlist = "";
        for (var i = 0; i < selectleng; i++) {
            idlist += selectdata[i].ToolID + ",";
        }
        idlist = idlist.substring(0, idlist.length - 1);
        if (selectleng > 0) {
            Deleteinfo(idlist);
        }
        // alert(table.row('.selected').getData);
    });

    $("#addBut").click(function () {
        location.href = "/Library/Add";
    });


    /* Get the rows which are currently selected */
    function fnGetSelected(oTable) {
        var aReturn = new Array();
        var aTrs = table.fnGetNodes();
        for (var i = 0; i < aTrs.length; i++) {
            if ($(aTrs[i]).hasClass('row_selected')) {
                aReturn.push(aTrs[i]);
            }
        }
        return aReturn;
    }

    $("#Searchbtn").bind("click", function () { //按钮 触发table重新请求服务器

        if ($("#isActive").val() == "1") {
            table.fnSetColumnVis(5, false);//隐藏列 
        }
        else if ($("#hidOneActionPageIsEdit").val() == "1") {
            table.fnSetColumnVis(5, true);//显示列 
        }
        table.fnDraw();
    });

    $("#regionselect").change(function () {
        var rid = $("#regionselect").val();
        if (rid == "") {
            $("#districtselect").empty();
        } else {
            $.ajax({
                data: { regionid: rid },
                type: "POST",
                url: "/Area/getdistrict",
                datatype: "json",
                success: function (data) {
                    var selectdata = data.date;
                    $("#districtselect").empty();
                    for (var i = 0; i < selectdata.length ; i++) {
                        $("#districtselect").append('<option value=' + selectdata[i].DistrictID + '>' + selectdata[i].DistrictName + '</option>');
                    }
                }
            });
        }
    });
});

function updatainfo(parameters) {

    //var chboxlist = $("input[name='infocheck']:checked");
    //alert(chboxlist.length);
    //alert(chboxlist[0].innerHTML);
    // var alldata = $('#ToolListDIV').dataTable().fnGetData();//得到页面中所有对象
    if (parameters != undefined) {
        location.href = "/Area/AreaUpdate/" + parameters;
    } else {
        var selectdata = $("#AreaTB").DataTable().rows('.selected').data();
        var selectleng = selectdata.length;
        if (selectleng == 1) {
            location.href = "/Area/AreaUpdate/" + selectdata[0].ToolID;
        } else {
            alert("只能选择一条数据");
        }
    }

}

function Deleteinfo(parameters) {
    $.confirm({
        title: '系统提示',
        content: '您确认要删除记录吗？',
        confirm: function () {
            $.ajax({
                type: "post",
                url: "/Area/Delinfo",
                data: { "idlist": parameters },
                success: function (data) {
                    if (data == "nouser") {
                        location.href = '/Login/Index';
                    } else {
                        table.fnDraw();
                    }
                }
            });
        },
        cancel: function () {
        }
    });
}